package com.af.dao.flight.impl;

import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;




import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.af.common.CommonUtil;
import com.af.common.DBHelper;
import com.af.common.Log4jXmlConfig;
import com.af.dao.flight.IRateManager;
import com.af.entity.flight.RateEntity;

public class RateManager implements IRateManager {

	Logger LOG = Log4jXmlConfig.getInstance().returnLogger(RateManager.class);
	private StringBuilder strBuilder = null;

	/**
	 * 替换SQL可用字符串
	 * @param rateNos
	 * @return
	 */
	private String returnRateNoStr(String rateNos) {
		if (rateNos.contains("/")) {
			rateNos = rateNos.replaceAll("/", "','");
		}
		return rateNos;
	}
	
	@Override
	public void addNewRate(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		map = (Map)JSONObject.toBean(JSONObject.fromObject(map.get("RateData")),Map.class);
		
		RateEntity rate = new RateEntity();
		
		rate.setRateNo(map.get("RateNo").toString());
		
		rate.setStatus(map.get("Status").toString());
		
		rate.setAgentName(map.get("AgentName").toString());
		
		rate.setRateType(map.get("RateType").toString());
		
		rate.setAdultsType(map.get("AdultsType").toString());
		
		rate.setVoyageType(map.get("VoyageType").toString());
		
		rate.setUserType(map.get("UserType").toString());
		
		rate.setTicketType(map.get("TicketType").toString());
		
		rate.setCheckType(map.get("CheckType").toString());
		
		rate.setScitys(map.get("Scitys").toString());
		
		rate.setEcitys(map.get("Ecitys").toString());
		
		rate.setAircom(map.get("Aircom").toString());
		
		rate.setFlightNos(map.get("FlightNos").toString());
		
		rate.setCabins(map.get("Cabins").toString());
		
		rate.setNotCabins(map.get("NotCabins").toString());
		
		rate.setSchedule(map.get("Schedule").toString());
		
		rate.setBeginDate(map.get("BeginDate").toString());
		
		rate.setSDate(map.get("SDate").toString());
		
		rate.setEDate(map.get("EDate").toString());
		
		rate.setDisCount(map.get("DisCount").toString());
		
		rate.setPayValue(map.get("PayValue").toString());
		
		rate.setSpeciaLimitEnumStr(map.get("specialLimitEnumStr").toString());
		
		rate.setIsUse(map.get("isUse").toString());
		
		rate.setRemark(map.get("Remark").toString());
		
		rate.setAgentFee(map.get("AgentFee").toString());
		
		rate.setPayConfig(map.get("PayConfig").toString());
		
		rate.setWorkTime(map.get("WorkTime").toString());
		
		rate.setEfficiency(map.get("Efficiency").toString());
		
		// 录入数据
		strBuilder = new StringBuilder();
		
		strBuilder.delete(0, strBuilder.length());
		
		strBuilder
				.append(
						" INSERT INTO Plane_Rates (rateNo,cabins,aircom,flightNos,scitys,ecitys,isUse,disCount,workTime,"
								+ "effciency,workStatus,agentName,rateType,adultsType,checkType,beginDate,ticketType,payConfig,"
								+ "agentFee,scheduLe,notCabins,sdate,edate,remark,userType,status,voyageType,specialLimitEnumStr,payValue)")
				.append(" VALUES('").append(rate.getRateNo()).append("',")
				.append("'").append(rate.getCabins()).append("',").append("'")
				.append(rate.getAircom()).append("',").append("'").append(
						rate.getFlightNos()).append("',").append("'").append(
						rate.getScitys()).append("',").append("'").append(
						rate.getEcitys()).append("',").append("'").append(
						rate.getIsUse()).append("',").append("'").append(
						rate.getDisCount()).append("',").append("'").append(
						rate.getWorkTime()).append("',").append("'").append(
						rate.getEfficiency()).append("',").append("'").append(
						rate.getWorkStatus()).append("',").append("'").append(
						rate.getAgentName()).append("',").append("'").append(
						rate.getRateType()).append("',").append("'").append(
						rate.getAdultsType()).append("',").append("'").append(
						rate.getCheckType()).append("',").append("'").append(
						rate.getBeginDate()).append("',").append("'").append(
						rate.getTicketType()).append("',").append("'").append(
						rate.getPayConfig()).append("',").append("'").append(
						rate.getAgentFee()).append("',").append("'").append(
						rate.getSchedule()).append("',").append("'").append(
						rate.getNotCabins()).append("',").append("'").append(
						rate.getSDate()).append("',").append("'").append(
						rate.getEDate()).append("',").append("'").append(
						rate.getRemark()).append("',").append(
						rate.getUserType()).append(",")
				.append(rate.getStatus()).append(",").append(
						rate.getVoyageType()).append(",'").append(
						rate.getSpeciaLimitEnumStr()).append("',").append(
						rate.getPayValue()).append(")");
		
		String resultSql = strBuilder.toString().replaceAll("null", "NULL")
				.replaceAll("''", "NULL").replaceAll("'NULL'", "NULL");
		
		update(resultSql);
	}

	@Override
	public void batchDeleteRates(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		
		strBuilder = new StringBuilder();
		strBuilder.delete(0, strBuilder.length());
		strBuilder.append("DELETE FROM Plane_Rates WHERE rateNo in ('");
		strBuilder.append(returnRateNoStr(map.get("RateData").toString())).append("')");
		update(strBuilder.toString());
	}

	@Override
	public void batchModifyRateEdate(HttpServletRequest request)
			throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		strBuilder = new StringBuilder();
		strBuilder.delete(0, strBuilder.length());
		strBuilder.append("UPDATE Plane_Rates SET edate='").append(map.get("SDate")).append("' ")
				.append("WHERE rateNo in('").append(returnRateNoStr(map.get("RateData").toString()))
				.append("')");
		update(strBuilder.toString());
	}

	@Override
	public void batchModifyRateSdate(HttpServletRequest request)
			throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		strBuilder = new StringBuilder();
		strBuilder.delete(0, strBuilder.length());
		strBuilder.append("UPDATE Plane_Rates SET sdate='").append(map.get("SDate")).append("' ")
				.append("WHERE rateNo in('").append(returnRateNoStr(map.get("RateData").toString()))
				.append("')");
		update(strBuilder.toString());
	}

	@Override
	public void modifyRate(HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		
		map = (Map)JSONObject.toBean(JSONObject.fromObject(map.get("RateData")),Map.class);
		
		RateEntity rate = new RateEntity();
		
		if (map.get("RateNo") != null) {
			rate.setRateNo(map.get("RateNo").toString());
		}
		
		if (map.get("Status") != null) {
			rate.setStatus(map.get("Status").toString());
		}
		
		if (map.get("AgentName") != null) {
			rate.setAgentName(map.get("AgentName").toString());
		}
		
		if (map.get("RateType") != null) {
			rate.setRateType(map.get("RateType").toString());
		}
		
		if (map.get("AdultsType") != null) {
			rate.setAdultsType(map.get("AdultsType").toString());
		}
		
		if (map.get("VoyageType") != null) {
			rate.setVoyageType(map.get("VoyageType").toString());
		}
		
		if (map.get("UserType") != null) {
			rate.setUserType(map.get("UserType").toString());
		}
		
		if (map.get("TicketType") != null) {
			rate.setTicketType(map.get("TicketType").toString());
		}
		
		if (map.get("CheckType") != null) {
			rate.setCheckType(map.get("CheckType").toString());
		}
		
		if (map.get("Scitys") != null) {
			rate.setScitys(map.get("Scitys").toString());
		}
		
		if (map.get("Ecitys") != null) {
			rate.setEcitys(map.get("Ecitys").toString());
		}
		
		if (map.get("Aircom") != null) {
			rate.setAircom(map.get("Aircom").toString());
		}
		
		if (map.get("FlightNos") != null) {
			rate.setFlightNos(map.get("FlightNos").toString());
		}
		
		if (map.get("Cabins") != null) {
			rate.setCabins(map.get("Cabins").toString());
		}
		
		if (map.get("NotCabins") != null) {
			rate.setNotCabins(map.get("NotCabins").toString());
		}
		
		if (map.get("Schedule") != null) {
			rate.setSchedule(map.get("Schedule").toString());
		}
		
		if (map.get("BeginDate") != null) {
			rate.setBeginDate(map.get("BeginDate").toString());
		}
		
		if (map.get("SDate") != null) {
			rate.setSDate(map.get("SDate").toString());
		}
		
		if (map.get("EDate") != null) {
			rate.setEDate(map.get("EDate").toString());
		}
		
		if (map.get("DisCount") != null) {
			rate.setDisCount(map.get("DisCount").toString());
		}
		
		if (map.get("PayValue") != null) {
			rate.setPayValue(map.get("PayValue").toString());
		}
		
		if (map.get("specialLimitEnumStr") != null) {
			rate.setSpeciaLimitEnumStr(map.get("specialLimitEnumStr")
					.toString());
		}
		
		if (map.get("isUse") != null) {
			rate.setIsUse(map.get("isUse").toString());
		}
		if (map.get("Remark") != null) {
			rate.setRemark(map.get("Remark").toString());
		}
		
		if (map.get("AgentFee") != null) {
			rate.setAgentFee(map.get("AgentFee").toString());
		}
		
		if (map.get("PayConfig") != null) {
			rate.setPayConfig(map.get("PayConfig").toString());
		}
		
		if (map.get("WorkTime") != null) {
			rate.setWorkTime(map.get("WorkTime").toString());
		}
		
		if (map.get("Efficiency") != null) {
			rate.setEfficiency(map.get("Efficiency").toString());
		}
		
		// 修改数据
		strBuilder = new StringBuilder();
		
		strBuilder.delete(0, strBuilder.length());
		
		strBuilder.append("UPDATE Plane_Rates SET ");
		
		if (CommonUtil.isNotNullOrEmpty(rate.getStatus())) {
			
			strBuilder.append(" status=").append(rate.getStatus()).append(",");
			
		}
		
		if (CommonUtil.isNotNullOrEmpty(rate.getAgentName())) {
			
			strBuilder.append(" agentName=");
			
			if (rate.getAgentName().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getAgentName()).append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getRateType())) {
			
			strBuilder.append(" rateType='").append(rate.getRateType()).append(
					
					"',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getAdultsType())) {
			
			strBuilder.append(" adultsType='").append(rate.getAdultsType())
			
					.append("',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getVoyageType())) {
			
			strBuilder.append(" voyagetype=");
			
			if (rate.getVoyageType().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getVoyageType())
				
						.append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getUserType())) {
			strBuilder.append(" userType=");
			
			if (rate.getUserType().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getUserType()).append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getTicketType())) {
			
			strBuilder.append(" ticketType='").append(rate.getTicketType())
			
					.append("',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getCheckType())) {
			
			strBuilder.append(" checkType='").append(rate.getCheckType())
			
					.append("',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getScitys())) {
			
			strBuilder.append(" scitys=");
			
			if (rate.getScitys().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getScitys()).append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getEcitys())) {
			
			strBuilder.append(" ecitys=");
			
			if (rate.getEcitys().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getEcitys()).append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getAircom())) {
			
			strBuilder.append(" aircom=");
			
			if (rate.getAircom().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getAircom()).append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getFlightNos())) {
			
			strBuilder.append(" flightNos='").append(rate.getFlightNos())
			
					.append("',");
			
		}
		
		if (CommonUtil.isNotNullOrEmpty(rate.getCabins())) {
			
			strBuilder.append(" cabins=");
			
			if (rate.getCabins().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getCabins()).append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getNotCabins())) {
			
			strBuilder.append(" notCabins='").append(rate.getUserType())
					.append("',");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getSchedule())) {
			
			strBuilder.append(" scheduLe='").append(rate.getSchedule()).append(
					"',");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getBeginDate())) {
			
			strBuilder.append(" beginDate='").append(rate.getBeginDate())
					.append("',");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getSDate())) {
			
			strBuilder.append(" sdate='").append(rate.getSDate()).append("',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getEDate())) {
			
			strBuilder.append(" edate='").append(rate.getEDate()).append("',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getDisCount())) {
			
			strBuilder.append(" discount=").append(rate.getDisCount()).append(
					",");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getPayValue())) {
			
			strBuilder.append(" payValue='").append(rate.getPayValue()).append(
					"',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getSpeciaLimitEnumStr())) {
			strBuilder.append(" specialLimitEnumStr=");
			
			if (rate.getSpeciaLimitEnumStr().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getSpeciaLimitEnumStr())
						.append("'");
				
			}
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getIsUse())) {
			
			strBuilder.append(" isUse='").append(rate.getIsUse()).append("',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getRemark())) {
			
			strBuilder.append(" remark=");
			
			if (rate.getRemark().trim() == "") {
				
				strBuilder.append("NULL");
				
			} else {
				
				strBuilder.append(" '").append(rate.getRemark()).append("'");
				
			}
			
			strBuilder.append(",");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getAgentFee())) {
			
			strBuilder.append(" agentFee='").append(
					!rate.getAgentFee().trim().equals("") ? rate.getAgentFee()
							.trim() : "0").append("',");
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getPayConfig())) {
			
			strBuilder.append(" payConfig='").append(rate.getPayConfig())
					.append("',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getWorkTime())) {
			
			strBuilder.append(" workTime='").append(rate.getWorkTime()).append(
					"',");
			
		}
		if (CommonUtil.isNotNullOrEmpty(rate.getEfficiency())) {
			
			strBuilder.append(" effciency='").append(rate.getEfficiency())
					.append("',");
			
		}
		// strBuilder.delete(strBuilder.length() - 1, strBuilder.length());
		strBuilder.append(" modifyTime='").append(
				CommonUtil.returnDateFormat("yyyy-MM-dd HH:mm:ss")).append("'");
		
		strBuilder.append(" WHERE rateNo='").append(rate.getRateNo()).append(
				"'");
		
		String resultSql = strBuilder.toString().replaceAll("null", "NULL")
				.replaceAll("''", "NULL").replaceAll("'NULL'", "NULL");
		
		update(resultSql);
	}

	private void update(String sql, Object... args) {
		DBHelper dh = DBHelper.getInstance();

		try {
			if (args != null) {

				dh.update(sql);

			} else {

				dh.update(sql, args);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		dh.close();
	}

	//批量增加返点
	@Override
	public void batchAddRateDiscount(HttpServletRequest request)
			throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		strBuilder = new StringBuilder();
		strBuilder.delete(0, strBuilder.length());
		strBuilder.append("UPDATE Plane_Rates SET discount=discount+").append(map.get("DataValue")).append(" ")
				.append("WHERE rateNo in('").append(returnRateNoStr(map.get("RateData").toString()))
				.append("')");
		update(strBuilder.toString());
	}

	//修改政策状态
	@Override
	public void batchModifyRateStatus(HttpServletRequest request)
			throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		
		strBuilder = new StringBuilder();
		strBuilder.delete(0, strBuilder.length());
		strBuilder.append("UPDATE Plane_Rates SET status='").append(map.get("DataValue")).append("' ")
				.append("WHERE rateNo in('").append(returnRateNoStr(map.get("RateData").toString()))
				.append("'");
		update(strBuilder.toString());
	}
	
	//修改政策返点
	@Override
	public void batchModifyRatesDiscount(HttpServletRequest request)
			throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		strBuilder = new StringBuilder();
		strBuilder.delete(0, strBuilder.length());
		strBuilder.append("UPDATE Plane_Rates SET disCount=").append(map.get("DataValue"))
				.append(" WHERE rateNo in('").append(returnRateNoStr(map.get("RateData").toString()))
				.append("')");
		update(strBuilder.toString());
	}
	
	//修改客票类型
	@Override
	public void batchModifyTicketType(HttpServletRequest request)
			throws Exception {
		// TODO Auto-generated method stub
		Map map = CommonUtil.returnParamMap(request);
		strBuilder = new StringBuilder();
		strBuilder.delete(0, strBuilder.length());
		strBuilder.append("UPDATE Plane_Rates SET ticketType='").append(map.get("DataValue")).append("' ")
				.append("WHERE rateNo in('").append(returnRateNoStr(map.get("RateData").toString()))
				.append("')");
		update(strBuilder.toString());
	}
}
